package com.example.haojiutao.service.impl;


import cn.hutool.crypto.digest.MD5;
import com.example.haojiutao.common.Result;
import com.example.haojiutao.exception.HaoJiuTaoException;
import com.example.haojiutao.mapper.UserMapper;
import com.example.haojiutao.model.domain.User;
import com.example.haojiutao.service.UserService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description: 用户业务处理实现类
 * @Author: cheng_jianzhen
 * @CreateTime: 2024/1/20 17:15
 */

@Slf4j
@Service

public class UserServiceImpl implements UserService {

@Autowired
    private UserMapper userMapper;

    /*
     * @Description: 注册用户
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/20 17:20
     * @param: user
     * @return: java.lang.String
     **/
    @Override
    public Result register(User user) {
        String userName =user.getUserName();
        String password=user.getUserPwd();
       String phoneNum= user.getPhoneNum();
        if(!StringUtils.hasLength(userName)
        || !StringUtils.hasLength(password)
        || !StringUtils.hasLength(phoneNum)
        ){
            log.warn("用户注册信息为空，userName ={},password ={}," +
                    "phoneNum={}",userName,password,phoneNum);
            return Result.faile(100001,"用户名，密码，手机号不能为空");
        }
        int users=userMapper.selectCountByUserName(userName);
        if(users>0){
            throw new HaoJiuTaoException("100005","用户名已存在，请更改");
        }
        log.info("注册成功");
        user.setCreateTime(new Date());
        //用户信息验证
        password = MD5.create().digestHex(user.getUserPwd());
        user.setUserPwd(password);
        int rows =userMapper.insert(user);
        if(rows==1){
            return Result.success("注册成功");
        }

        return Result.faile("失败");
    }
    /*
     * @Description: 用户个人信息查案，注册登录
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/20 17:31
     * @return: com.example.musicserver.common.Result
     **/
    @Override
    public User selectById(long userId) {
        return null;
    }


    /*
     * @Description: TODO用户修改个人信息
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/20 17:36
     * @param: user
     * @return: com.example.musicserver.common.Result
     **/
//    @Override
//    public Result updateUser(User user) {

//        String phoneNum= user.getPhoneNum();
//        if(!StringUtils.hasLength(phoneNum)){
//
//            log.error("用户注册信息为空，userId={}",user.getId());
//            return Result.faile(100002,"手机号不能为空");
//        }
//        user.setUpdateTime(new Date());
//        int rows =userMapper.updateUser(user);
//        if(rows==1){
//            return Result.success("修改成功");
//        }
//        return Result.faile("失败");
//    }


    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    /*
        TODO根据用户ID删除用户记录
        * @Author: cheng_jianzhen
        * @CreateTime: 2024/1/20 17:47
        * @param: id
        * @return: com.example.musicserver.common.Result
        **/
    @Override
    public int deleteById(Integer id){
//        物理删除
//        int rows=userMapper.deleteById(id);
        //逻辑删除
//        User user=new User();
//        user.setId(id);
//        user.setDelTlag(1);
//        user.setUpdateTime(new Date());
//        int rows =userMapper.updateUser(user);
//        if(rows==1){
//            return Result.success("删除成功");
//        }
//        return Result.faile("删除失败");
        return userMapper.deleteById(id);
    }


    /*
     * @Description: 列表分页查询用户
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/20 18:16
     * @param: user
     * @return: java.util.List<com.example.secondhand.domain.User>
     **/
    @Override
    public List<User> listByUser(User user){
        PageHelper.startPage(user.getPageNum(),user.getPageSize());//在apifox中自行输入pageNUm,pageSize的参数
        List<User> users=userMapper.selectListByUser(user);
        return users;

    }
//根据用户ID查询用户
    @Override
    public User getById(Integer id) {
        return userMapper.selectById(id);
    }
    /*
     * @Description: 通过用户ID查询用户信息
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/20 18:23
     * @param: userId
     * @return: com.example.haojiutao.model.domain.User
     **/
    @Override
    public User getUserInfoById(int userId) {
        User user=userMapper.selectById(userId);
        return user;
    }

    @Override
    public User getUserInfoByName(String userName) {
        User user=userMapper.selectByName(userName);
        return user;
    }

    @Override
    public User login(String phoneNum, String userPwd) {
        User user = userMapper.selectByPhonePassword(phoneNum,MD5.create().digestHex(userPwd));
        return user;
    }

    @Override
    public List<User> getRecommendUseravg(User user) {

        return userMapper.selectRecommendUseravg(user);
    }

    @Override
    public Result insertuser(User user) {
        Integer id = user.getId();
        int rows = 0;
        if(null == id || id == 0){
            rows = userMapper.insertuser(user);
        }else{
            rows = userMapper.updateUser(user);
        }
        if(rows == 1){
            return Result.success("保存成功");
        }else{
            return Result.faile("保存失败");
        }
    }
}
